import sys
input=lambda:sys.stdin.readline().rstrip()
n=int(input())
A=list(map(int,input().split()))
pos=[[],[]]
for i in range(n):
pos[A[i]].append(i)
k=sum(A)
ans=[0]+[float('inf') for i in range(k)]
for i in range(n-k):
for j in range(k,0,-1):
ans[j]=min(ans[j],ans[j-1]+abs(pos[0][i]-pos[1][j-1]))
print(ans[k])
#include <bits/stdc++.h>
using namespace std;
constexpr int INF = 0x3f3f3f3f;
int main() {
cin.sync_with_stdio(false);
int n;
cin >> n;
vector<int> people, chair;
for (int i = 0, cur = 0; i < n; ++i) {
cin >> cur;
if (cur) {
people.push_back(i);
} else {
chair.push_back(i);
}
}
int n1 = people.size(), n2 = chair.size();
vector<vector<int>> dp(n2 + 1, vector<int>(n1 + 1, INF));
for (int i = 0; i <= n2; ++i) {
dp[i][n1] = 0;
}
for (int i = n2 - 1; i >= 0; --i) {
for (int j = n1 - 1; j >= 0 && n1 - j <= n2 - i; --j) {
dp[i][j] = min(dp[i + 1][j + 1] + abs(chair[i] - people[j]), dp[i + 1][j]);
}
}
cout << dp[0][0];
return 0;
}
714B - Filya and Homework | 31A - Worms Evolution |
1691A - Beat The Odds | 433B - Kuriyama Mirai's Stones |
892A - Greed | 32A - Reconnaissance |
1236D - Alice and the Doll | 1207B - Square Filling |
1676D - X-Sum | 1679A - AvtoBus |
1549A - Gregor and Cryptography | 918C - The Monster |
4B - Before an Exam | 545B - Equidistant String |
1244C - The Football Season | 1696B - NIT Destroys the Universe |
1674A - Number Transformation | 1244E - Minimizing Difference |
1688A - Cirno's Perfect Bitmasks Classroom | 219A - k-String |
952A - Quirky Quantifiers | 451B - Sort the Array |
1505H - L BREAK into program | 171E - MYSTERIOUS LANGUAGE |
630D - Hexagons | 1690D - Black and White Stripe |
1688D - The Enchanted Forest | 1674C - Infinite Replacement |
712A - Memory and Crow | 1676C - Most Similar Words |